package com.example.bankend.service.impl;

import com.example.bankend.entity.Saving;
import com.example.bankend.entity.User;
import com.example.bankend.mapper.SavingMapper;
import com.example.bankend.mapper.UserMapper;
import com.example.bankend.service.SavingService;
import com.example.bankend.service.UserService;
import com.example.bankend.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author luolinila
 * @date 2025/6/18 09:52
 */
@Service
public class SavingServerIpml implements SavingService {
    @Autowired
    private SavingMapper savingMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Long createSaving(Long adminID, String adminName, String adminPass, String savingName, String descrip) {
        try {
            User admin = userMapper.selectAdmin();
            if (!adminName.equals(admin.getCustomerName()) || adminID.compareTo(admin.getCustomerID()) != 0 || !adminPass.equals(admin.getPass())) {
                return -1L;
            }
            Saving tempSaving = savingMapper.selectSavingByName(savingName);
            if (tempSaving != null && "0".equals(tempSaving.getIsDelete())) {
                return 10004L;
            }
            List<Long> savingIdList = selectAllSavingID();
            Long savingId = Random.randomLong();
            while (savingIdList.contains(savingId)) {
                savingId = Random.randomLong();
            }
            Saving saving = new Saving(savingId, savingName, descrip, "0");
            savingMapper.insertSaving(saving);
            savingIdList.add(savingId);
            redisTemplate.opsForValue().set("allSavingID", savingIdList, 1, TimeUnit.HOURS);
            return savingId;
        } catch (Exception e) {
            System.out.println(e);
            return -1L;
        }
    }

    @Override
    public Saving getSavingById(Long id) {
        try {
            Saving saving = savingMapper.selectSavingById(id);
            if (saving == null || "1".equals(saving.getIsDelete())) {
                return null;
            }
            return saving;
        }catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    @Override
    public Saving getSavingByName(String savingName) {
        try {
            Saving saving = savingMapper.selectSavingByName(savingName);
            if (saving == null || "1".equals(saving.getIsDelete())) {
                return null;
            }
            return saving;
        }catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    @Override
    public Long deleteSaving(Long adminID, String adminName, String adminPass, Long savingID) {
        User admin = userMapper.selectAdmin();
        try {
            if (!adminName.equals(admin.getCustomerName()) || adminID.compareTo(admin.getCustomerID()) != 0 || !adminPass.equals(admin.getPass())) {
                return -1L;
            }
            if (getSavingById(savingID) == null && "1".equals(getSavingById(savingID).getIsDelete())) {
                return 10004L;
            }
            savingMapper.deleteSaving(savingID);
            return 200L;
        } catch (Exception e) {
            System.out.println(e);
            return -1L;
        }
    }

    @Override
    public List<Long> selectAllSavingID() {
        List<Long> list = (List<Long>) redisTemplate.opsForValue().get("allSavingID");
        if (list == null) {
            list = savingMapper.selectAllSavingID();
        }
        redisTemplate.opsForValue().set("allSavingID", list, 1, TimeUnit.HOURS);
        return  list;
    }

    @Override
    public Long recoverSaving(Long savingID) {
        try {
            Saving saving = savingMapper.selectSavingById(savingID);
            if (saving == null) {
                return -1L;
            }
            savingMapper.recoverSaving(savingID);
            return savingID;
        } catch (Exception e) {
            System.out.println(e);
            return -1L;
        }
    }

    @Override
    public List<Saving> getAllSaving(Long adminID, String adminName, String adminPass) {
        try {
            User admin = userMapper.selectAdmin();
            if (!adminName.equals(admin.getCustomerName()) || adminID.compareTo(admin.getCustomerID()) != 0 || !adminPass.equals(admin.getPass())) {
                return null;
            }
            return savingMapper.selectAll();
        } catch (Exception e) {
            System.out.println(e);
            return null;
        }
    }

    @Override
    public Long updateSaving(Long adminID, String adminName, String adminPass, Long savingID, String savingName, String descrip) {
        try {
            User admin = userMapper.selectAdmin();
            if (!adminName.equals(admin.getCustomerName()) || adminID.compareTo(admin.getCustomerID()) != 0 || !adminPass.equals(admin.getPass())) {
                return null;
            }
            Saving saving = savingMapper.selectSavingById(savingID);
            if (saving == null || "1".equals(saving.getIsDelete())) {
                return 10004L;
            }
            savingMapper.updateNameAndDes(savingID, savingName, descrip);
            return savingID;
        } catch (Exception e) {
            System.out.println(e);
            return -1L;
        }
    }
}
